<@pp.dropOutputFile/>
<#include "../common/util.ftl">
<#-- iterate table list-->
<#list doc.tables as table>

<#-- generate package name -->
<#assign app = doc.artifactid?replace(".", "/")>
<#assign package = table.moduleid?replace(".", "/")>
<#assign fileName = table.name?cap_first>
<@pp.changeOutputFile name = "/" + package  + "/" + fileName + "ServiceSqliteImpl.vb"/>
<#compress>
<#list table.fields as field>
	<#assign typePackage = getTypePackage(field.type)>
	<#if typePackage?length != 0>Imports ${typePackage}</#if>
</#list>
</#compress>
Imports Dapper
Imports System.Reflection
Imports Stone.Common
Imports NPOI.HSSF.UserModel
Imports System.IO

Namespace ${package?replace("/", ".")}
<#assign package = package + "." + fileName>

    ''' <summary>
	''' ${fileName}服务层实现
    ''' ${table.comment}
    ''' </summary>
    ''' <remarks>${doc.version}</remarks>
    Public Class ${fileName}ServiceSqliteImpl
        Implements I${fileName}Service

        Private _Log As log4net.ILog = log4net.LogManager.GetLogger(Reflection.MethodBase.GetCurrentMethod().DeclaringType)
		Private _${fileName}Dao As I${fileName}Dao = Nothing
	<#list table.fields as field>
		<#if field.map_type?? && ((field.map_type == 1) || (field.map_type == 2))>
		Private _${field.map_class?cap_first}Dao As I${field.map_class?cap_first}Dao = Nothing
		</#if>
	</#list>
	
        Public Sub New(connection As IDbConnection)
			'连接数据库
			_${fileName}Dao = New ${fileName}DaoSqliteImpl(connection)
		<#list table.fields as field>
			<#if field.map_type?? && ((field.map_type == 1) || (field.map_type == 2))>
			_${field.map_class?cap_first}Dao = New ${field.map_class?cap_first}DaoSqliteImpl(connection)
			</#if>
		</#list>
        End Sub

        Public Function create(obj As ${fileName}) As Outcome Implements I${fileName}Service.create
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res.MessageId = "${app}.${package + ".Service.input"}"
				If(obj IsNot Nothing) Then
					res = _${fileName}Dao.create(obj)
				End If
			End If
			
            Return res
        End Function

        Public Function createBatch(objs As List(Of ${fileName})) As Outcome Implements I${fileName}Service.createBatch
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res.MessageId = "${app}.${package + ".Service.input"}"
				If(objs IsNot Nothing) Then
					res = _${fileName}Dao.createBatch(objs)
				End If
			End If
			
            Return res
        End Function

        Public Function update(obj As ${fileName}) As Outcome Implements I${fileName}Service.update
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res.MessageId = "${app}.${package + ".Service.input"}"
				If(obj IsNot Nothing) Then
					res = _${fileName}Dao.update(obj)
				End If
			End If
			
            Return res
        End Function

        Public Function delete(ids As List(Of String)) As Outcome Implements I${fileName}Service.delete
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res = _${fileName}Dao.delete(ids)
			End If
			
            Return res
        End Function

        Public Function delete(id As String) As Outcome Implements I${fileName}Service.delete
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res = _${fileName}Dao.delete(id)
			End If
			
            Return res
        End Function

        Public Function delete(params As Dictionary(Of String, Object)) As Outcome Implements I${fileName}Service.delete
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res = _${fileName}Dao.delete(params)
			End If
			
            Return res
        End Function

        Public Function getPage(page As Integer, size As Integer, params As Dictionary(Of String, Object)) As Outcome Implements I${fileName}Service.getPage
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res = _${fileName}Dao.getPage(page, size, params)
			End If
			
            Return res
        End Function

        Public Function getOne(params As Dictionary(Of String, Object)) As Outcome Implements I${fileName}Service.getOne
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res.MessageId = "${app}.${package + ".Service.input"}"
				If(params IsNot Nothing) Then
					res = _${fileName}Dao.getOne(params)
				End If
			End If
			
            Return res
        End Function

        Public Function size(params As Dictionary(Of String, Object)) As Outcome Implements I${fileName}Service.size
            Dim res As Outcome = New Outcome(False)
			res.MessageId = "${app}.${package + ".Service.inject.fail"}"
			
			If(_${fileName}Dao IsNot Nothing) Then
				res = _${fileName}Dao.size(params)
			End If
			
            Return res
        End Function
	
		Function toExcel(data As List(Of ${fileName}), file As String) As Outcome Implements I${fileName}Service.toExcel
            Dim res As Outcome = New Outcome(False)
			If (Not IsNothing(data)) Then
				Try
					'Construct excel
					Dim field_count = ${table.fields?size}
					Dim workbook2003 As New HSSFWorkbook()
					workbook2003.CreateSheet("Sheet1")
					Dim SheetOne As HSSFSheet = DirectCast(workbook2003.GetSheet("Sheet1"), HSSFSheet)
					For i As Integer = 0 To data.Count
						SheetOne.CreateRow(i)
					Next
					'Export header
					Dim header As HSSFRow = DirectCast(SheetOne.GetRow(0), HSSFRow)
					Dim header_cell As HSSFCell() = New HSSFCell(field_count) {}
					For i As Integer = 0 To field_count - 1
						header_cell(i) = DirectCast(header.CreateCell(i), HSSFCell)
					Next
					<#list table.fields as field>
					header_cell(${field_index}).SetCellValue("${field.label}")
					</#list>

					'Export data
					For index As Integer = 1 To data.Count
						Dim item As ${fileName} = data.ElementAt(index - 1)
						Dim row As HSSFRow = DirectCast(SheetOne.GetRow(index), HSSFRow)
						Dim row_cell As HSSFCell() = New HSSFCell(field_count) {}
						For i As Integer = 0 To field_count - 1
							row_cell(i) = DirectCast(row.CreateCell(i), HSSFCell)
						Next
						<#list table.fields as field>
						row_cell(${field_index}).SetCellValue(item.${field.name?cap_first}.ToString())
						</#list>
					Next

					Dim file2003 As New FileStream(file, FileMode.Create)
					workbook2003.Write(file2003)
					file2003.Close()
					workbook2003.Close()
					res.MessageId = "${app}.${package + ".Service.toExcel.success"}"
				Catch ex As Exception
					res.MessageId = "${app}.${package + ".Service.toExcel.fail"}"
					res.Detail = ex.Message
					_Log.Error(ex.Message)
				End Try
			End If
            Return res
    End Function

	Public Sub loadBuddies(ByRef data As ${fileName}) Implements I${fileName}Service.loadBuddies
		If (Not IsNothing(data)) Then
			Dim params As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
			
		<#list table.fields as field>
			<#if field.map_type?? && field.map_type == 1>
			params.Clear()
			params.Add("id", data.${field.name?cap_first})
			data.Data_${field.name?cap_first} = _${field.map_class?cap_first}Dao.getOne(params).Data
			If(IsNothing(data.Data_${field.name?cap_first})) Then
				data.Data_${field.name?cap_first} = New ${field.map_class?cap_first}()
			End If
			
			</#if>
			<#if field.map_type?? && field.map_type == 2>
			params.Add("${fileName?lower_case}_id", data.${field.name?cap_first})
			data.Data_${field.name?cap_first} = data._${field.map_class?cap_first}Dao.getPage(params).Data
			If(IsNothing(data.Data_${field.name?cap_first})) Then
				data.Data_${field.name?cap_first} = New List(Of ${field.map_class?cap_first})()
			End If
			
			</#if>
		</#list>
		End If
	End Sub
	
	Public Sub saveBuddies(ByRef data As ${fileName}) Implements I${fileName}Service.saveBuddies
		If (Not IsNothing(data)) Then
		<#list table.fields as field>
			<#if field.map_type?? && field.map_type == 1>
			_${field.map_class?cap_first}Dao.create(data.Data_${fileName?cap_first}_ID)
			</#if>
			
			<#if field.map_type?? && field.map_type == 2>
			If(data.Data_${fileName?cap_first}_ID IsNot Nothing) Then
				For Each o In data.Data_${fileName?cap_first}_ID
					_${field.map_class?cap_first}Dao.create(o)
				Next
			
			End If
			</#if>
		</#list>
		End If
	End Sub
	
	Public Sub deleteBuddies(ByRef data As ${fileName}) Implements I${fileName}Service.deleteBuddies
		If (Not IsNothing(data)) Then
			Dim params As Dictionary(Of String, Object) = New Dictionary(Of String, Object)()
		<#list table.fields as field>
			<#if field.map_type?? && field.map_type == 1>
			_${field.map_class?cap_first}Dao.delete(data.${fileName?cap_first}_ID)
			</#if>
			
			<#if field.map_type?? && field.map_type == 2>
			params.Add("${fileName?lower_case}_id", data.${field.name?cap_first})
			data._${field.map_class?cap_first}Dao.delete(params)
			</#if>
		</#list>
		End If
	End Sub
	
    End Class
End Namespace

</#list>